package com.stu.student_back_end.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.stu.student_back_end.common.Constants;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblMoveAndOut;
import com.stu.student_back_end.entity.TblNoHere;
import com.stu.student_back_end.entity.TblStudent;
import com.stu.student_back_end.service.ITblStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生controller层
 */

@RestController
public class StudentController {

    @Autowired
    public ITblStudentService studentService;

    /**
     * 添加一条学生记录
     * @param student
     * @return
     */
    @PostMapping("/save")
    public Result saveStudent(@RequestBody TblStudent student) {
        boolean save = studentService.save(student);
        //查询为null,则返回失败
        if(!save) return Result.error(Constants.CODE_500,"");
        //查询不为null
        return Result.success("注册成功");
    }

    /**
     * 分页查询学生数据
     * @param pageDTO
     * @return
     */
    @PostMapping("/s/query")
    public Result queryStudent(@RequestBody PageDTO pageDTO){
        return Result.success(studentService.queryStudent(pageDTO));
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @PostMapping("/s/byId/{id}")
    public Result queryByIdStudent(@PathVariable("id") int id) {
        TblStudent student = studentService.getById(id);
        if(student == null) return Result.success("无此数据！");
        return Result.success("查询成功！",student);
    }

    /**
     * 修改指定id的数据
     * @param student
     * @return
     */
    @PostMapping("/s/update")
    public Result updateByIdStudent(@RequestBody TblStudent student) {
        boolean b = studentService.updateById(student);

        if(!b) return Result.error(Constants.CODE_500,"修改失败！");

        return Result.success("修改成功！");
    }

    /**
     * 根据id删除学生记录
     * @return
     */
    @DeleteMapping("/s/del/{id}")
    public Result delByIdStudent(@PathVariable("id") int id) {
        boolean removeById = studentService.removeById(id);

        if(!removeById) return Result.error(Constants.CODE_500,"删除失败！");

        return Result.success("删除成功！");
    }

    /**
     * 根据id修改状态
     * @param //moveAndOutVo
     * @return
     */
    @PostMapping("/s/mo/{id}")
    public Result updateMoveAndOut(@RequestBody TblMoveAndOut moveAndOutVo , @PathVariable("id") int id) {
        studentService.updateStudentState(moveAndOutVo, id);

        return Result.success("修改成功！");
    }

    @PostMapping("/s/nh")
    public Result updateNoHere(@RequestBody TblNoHere noHere) {
        boolean save = studentService.addStudentNoHere(noHere);

        if(!save) return Result.error(Constants.CODE_500,"添加失败！");

        return Result.success("添加成功");
    }

    /**
     * 根据id修改密码
     * @param id
     * @param password
     * @return
     */
    @GetMapping("/s/updatePwd/{id}/{password}")
    public Result updateStudentPwd(@PathVariable("id") int id, @PathVariable("password") String password) {

        boolean update = studentService.update(new LambdaUpdateWrapper<TblStudent>()
                .eq(TblStudent::getId, id)
                .set(TblStudent::getPassword, password));

        //为false 修改失败
        if(!update) return Result.error(Constants.CODE_500,"修改失败！");

        return Result.success("修改成功！");
    }
}
